package com.spark.actions.sys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;

import org.apache.ibatis.session.SqlSession;

import com.spark.core.Message;
import com.spark.core.SparkContext;
import com.spark.dao.SessionFactory;

public class MenuAction extends DefaultAction{
	
	public static final String FIELD_MENU_ID = "MENU_ID";
	public static final String FIELD_MENU_CODE = "MENU_CODE";
	public static final String FIELD_MENU_NAME = "MENU_NAME";
	public static final String FIELD_MENU_TYPE = "MENU_TYPE";
	public static final String FIELD_COMMAND_TYPE = "COMMAND_TYPE";
	public static final String FIELD_PARENT_ID = "PARENT_ID";
	public static final String FIELD_SEQUENCE = "SEQUENCE";
	
	/**
	 * 创建菜单.
	 * @param context
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void createMenu(SparkContext context) throws Exception {
		doExecute(context, "sys_menu.insert", ACTION_INSERT);
	}
	
	/**
	 * 更新菜单.
	 * @param context
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void updateMenu(SparkContext context) throws Exception {
		doExecute(context, "sys_menu.update", ACTION_UPDATE);
	}
	
	@SuppressWarnings("unchecked")
	private boolean validateScreen(Map map, Message message){
		return isNotEmpty(map.get(FIELD_MENU_CODE), message,"菜单编码不能为空!")||
			   isNotEmpty(map.get(FIELD_MENU_NAME), message,"菜单名称不能为空!")||
			   isNotEmpty(map.get(FIELD_PARENT_ID), message,"父菜单不能为空!");
	}
	
	
	@SuppressWarnings("unchecked")
	protected void doExecute(SparkContext context, String statement, String action) throws Exception{
		Map requestData = getRequestData(context);
		List list = (List)requestData.get(PARAMETER);
		Message message = new Message(true);
		if(list!=null && list.size() > 0){
			Map map = (Map)list.get(0);
			if(validateScreen(map,message)){
				SqlSession sqlSession = SessionFactory.getSession();
				try {
					setDefault(map,FIELD_SEQUENCE,0);
					String type = (String)map.get(FIELD_MENU_TYPE);
					if("F".equals(type)){
						map.put(FIELD_COMMAND_TYPE, null);
					}
					Integer count = (Integer)sqlSession.selectOne("sys_menu.selectDuplicateMenu", map);
					if(count>0){
						message.setWarning("存在相同编码的菜单!");
						message.setWidth(300);
						message.setHeiht(150);
					}else{
						doAction(sqlSession, action, statement,map);
					}
				} finally {
					SessionFactory.closeSession(sqlSession);
				}
			}
		}
		writeMessage(context, message);
	}
	
	
	/**
	 * 查询用户所有角色的菜单集合.
	 * TODO:考虑以后通过缓存来处理
	 * @param context
	 * @throws ServletException
	 * @throws IOException
	 */
	@SuppressWarnings("unchecked")
	public void selectUserMenus(SparkContext context)throws ServletException, IOException {
		Map<String, Object> model = context.getModel();
		SqlSession sqlSession = SessionFactory.getSession();
		try {
			List menus = new ArrayList();
			List temp = new ArrayList();
			Map map = new HashMap();
			setUserId(map, context);
//			map.put(UserAction.FIELD_USER_ID, getUserId(context));
			List roles = (List) sqlSession.selectList("sys_user_role.selectRoles",map);
			if(roles != null){
				map.put("roles", roles);
				List items = (List) sqlSession.selectList("sys_role_menu.selectUserMenus", map);
				if(items!=null){
					Iterator it = items.iterator();
					while(it.hasNext()){
						Map menu = (Map)it.next();
						Integer menuId = (Integer)menu.get(FIELD_MENU_ID);
						if(!temp.contains(menuId)){
							temp.add(menuId);
							menus.add(menu);
						}
					}
				}
			}
			model.put("sys_menus", menus);
		} finally {
			SessionFactory.closeSession(sqlSession);
		}
	}
}
